package com.jsjosem.aftersales.ui.worktickets;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.jsjosem.aftersales.R;
import com.jsjosem.aftersales.adapter.ProductCheckedAdapter;
import com.jsjosem.aftersales.data.response.CustomerResponse;
import com.jsjosem.aftersales.data.response.ProductResponse;
import com.jsjosem.aftersales.request.ProductRequest;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

public class ProductListActivity extends AppCompatActivity {

    private ImageView imgBack;
    private ListView productLv;
    private List<ProductResponse> productData;
    private ProductCheckedAdapter adapter;
    private TextView productFinish;
    private Intent intent;
    private SmartRefreshLayout smartRefreshLayout;
    private final ProductRequest productRequest = new ProductRequest();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_list);
        init();

        //点击listview的item
        productLv.setOnItemClickListener((adapterView, view, i, l) -> {
            ProductResponse productItem = productData.get(i);
            productItem.setItemChecked(!productItem.getItemChecked());
            adapter.notifyDataSetChanged();
        });

        //点击返回按钮
        imgBack.setOnClickListener(view -> {
            finish();
        });

        //点击完成按钮
        productFinish.setOnClickListener(view -> {
            ArrayList<ProductResponse> result = new ArrayList<>();
            for (ProductResponse productDatum : productData) {
                if (productDatum.getItemChecked()) {
                    result.add(productDatum);
                }
            }
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("result", result);
            intent.putExtras(bundle);
            setResult(RESULT_OK, intent);
            finish();
        });

        smartRefreshLayout.setEnableRefresh(false);
        smartRefreshLayout.setEnableLoadMore(false);

    }

    @Override
    protected void onStart() {
        super.onStart();
        CustomerResponse customerItem = (CustomerResponse) intent.getSerializableExtra("customerItem");
        getData(customerItem.getId());
    }

    private void init() {
        imgBack = findViewById(R.id.product_list_img_back);
        productLv = findViewById(R.id.product_list_lv);
        productFinish = findViewById(R.id.product_list_finish_tv);
        smartRefreshLayout = findViewById(R.id.refreshLayout);

        productData = new ArrayList<>();
        intent = getIntent();

        adapter = new ProductCheckedAdapter(ProductListActivity.this, R.layout.checked_item_list, productData);
        productLv.setAdapter(adapter);
    }

    private void getData(int id) {
        productRequest.getCustomerProducts(id, (res) -> {
            if (res.getData().size() != 0) {
                productData.addAll(res.getData());
                adapter.notifyDataSetChanged();
            } else {
                Toast.makeText(ProductListActivity.this, "该客户没有产品", Toast.LENGTH_SHORT).show();
            }
        }, failedMessage -> Toast.makeText(this, failedMessage.toString(), Toast.LENGTH_LONG).show());

    }


}